<?php

class Default_Model_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'user';
    public function getDataByUsername($username){
    	$query = $this->select()
                    ->from($this->_name,array('userId', 'username', 'password', 'status'))
                    ->where('username = ?', $username);
        return $this->fetchRow($query);
    }
    public function getDataById($userId){
    	$query = $this->select()
                        ->from($this->_name, array('userId', 'username', 'status'))
                        ->where('userId = ?',(int)$userId);
        return $this->fetchRow($query);
    }
    public function updateStatus($status, $userId){
        $data['status']=$status;
        return $this->update($data, 'userId=' . (int)$userId );
    }
    public function updatePass($userId, $password){
        $data['password'] = md5($password);
        return $this->update($data, 'userId=' . (int)$userId );
    }
     public function getAllData(){
    	$query = $this->select()
                        ->from($this->_name,array('userId', 'username', 'status'))
                        ->where("userId > 1");
    	return $this->fetchAll($query);
    }
    public function addData($username, $password, $status){
    	$data = array('username' => $username, 'password' => md5($password), 'status' => $status);
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function deleteData($userId){
        $this->delete('userId =' . (int)$userId);  
    }
    public function updateData($userId, $username, $password, $status){
    	if($password && strlen($password) >= 6){
    		 $data = array('username' => $username ,'password' => md5($password), 'status' => $status);
    	}else{
    		 $data = array('username' => $username, 'status' => $status);
    	}
        return $this->update($data, 'userId = ' . (int)$userId);
    }
    
  
}

